package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.*;
import com.example.communist_app_api.db.dto.PermissionDTO;

import com.example.communist_app_api.db.pojoDto.Permission;
import com.example.communist_app_api.service.AuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/12/4 12:03
 */
@Transactional
@Service
public class AuthorizationServiceImpl implements AuthorizationService {
    /*
    传入参数user实体，查询权限并返回
     */
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private ModuleDao moduleDao;
    @Autowired
    private ActionDao actionDao;

    @Override
    public List<PermissionDTO> getPermissionByUser(Integer userId) {
        //根据userId查询user_role表中user拥有的角色role，获取roleId
        //根据roleId查询permission_role表中获取permissionId，查询该角色拥有的权限。
        //根据权限信息组合成permissionDTO即可
        ArrayList<Integer> RoleIdList = userRoleDao.getRoleIdByUserId(userId);
        List<Integer> permissionIdList = new ArrayList<>();
        HashSet<PermissionDTO> permissionDTOHashSet = new HashSet<>();
        for (Integer e : RoleIdList){
            ArrayList<Integer> permissionIdByRoleId = rolePermissionDao.getPermissionIdByRoleId(e);
            for (Integer i:permissionIdByRoleId) {
                permissionIdList.add(i);
            }
        }
        for (Integer i : permissionIdList) {
            Permission permission = permissionDao.getPermissionByPermission(i);

            PermissionDTO permissionDTO = new PermissionDTO();

            if (!(permission.getModuleId() == 0 || permission.getActionId() ==0)) {
                String moduleCode = moduleDao.getModuleCodeByModuleId(permission.getModuleId());
                String actionCode= actionDao.getActionCodeByActionId(permission.getActionId());
                permissionDTO.setPermissionCode(moduleCode.concat("_").concat(actionCode));
            }

            permissionDTO.setId(permission.getId());
            permissionDTO.setPermissionName(permission.getPermissionName());
            permissionDTO.setPermissionType(permission.getPermissionType());
            permissionDTO.setComponent(permission.getComponent());
            permissionDTO.setPath(permission.getPath());
            permissionDTO.setParentId(permission.getParentId());

            permissionDTOHashSet.add(permissionDTO);
        }
        //set转List
        List<PermissionDTO> permissionDTOList = new ArrayList<>(permissionDTOHashSet);

        List<PermissionDTO> resultMenuList = new ArrayList<>();

        //构造菜单树
        //三层结构，从大到小来查
        for (PermissionDTO permissionDTO  : permissionDTOList) {
            //分流，组成一颗树，返回的是树的结构
            // 寻找子节点
            for (PermissionDTO e : permissionDTOList) {
                if (e.getParentId()==permissionDTO.getId()) {
                    permissionDTO.getChildren().add(e);
                }
            }
            // 判断父节点，添加到集合
            if(permissionDTO.getParentId()==0){
                resultMenuList.add(permissionDTO);
            }
        }

        return resultMenuList;
    }
}
